#!/usr/bin/perl
###############################################################################
# Version:  0.01-Beta (6th February 2006)
# Author:  R2Hosting, Melbourne. Australia
# Contact: info@r2hosting.com.au
# FileName: checkJOSVersions.pl
# Required Modules: File::Find::Rule
#
# Information/Overview:
# This is a quick and (very) dirty check to find Joomla/Mambo and several Component installations in the /home directory. It makes
# use of the version.php file to locate relavent information and send the output to optionally) STDOUT and to a designated email.
# Originally posted in the Joomla Forums, please post any modifications or improvements to assist others in the promotion and execution 
# of Joomla and the Open Source cause.
###############################################################################
# This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
###############################################################################
use POSIX;
use File::Find::Rule;
use MIME::Lite;

# Complete Your Choices/Details Here:
$outputresults          =       '1'; # Output results to STDOUT?
$emailresults           =       '1'; # Email the results
$searchpath             =       '/home'; # Path to start search (recursive)
$admin_email            =       'someone@somewhere.com.au'; # Enter your e-Mail address
$subject                =       "Joomla/Mambo and Component Version Check\n"; # Subject line for your email

  # Sets this Scripts Priority (20 lowest, -20 highest)
setpriority(PRIO_PROCESS,0,19);


# No Need To Do Anything Below Here --------------------------------------------------------------------------------------------------------------------------------
# Unless you wish to change the filename the script is looking for (version.php in this case)

# Declare Variables
my $product;
my $ver;
my $devstat;
my $devlev;
my $build;
my $codename;
my $reldate;

# Start STDOUT
if($outputresults == 1){ print "Starting Search, This will take a while..."; }

# Start the main run...
my @files = File::Find::Rule->file
                            # This is the file with all the version info in it
                            ->name("version.php")
                            ->in("$searchpath");
print "Search Complete...\n";

# Find the information we want in each iteration of the file
foreach $file (@files){
        undef $product;
        undef $ver;
        undef $devstat;
        undef $devlev;
        undef $build;
        undef $codename;
        undef $reldate;
        open(FM,"$file");

          while (<FM>) {
          # Line 1 = Joomla!, later Mambo's and several components
          # Line 2 = Early Mambo's

          # Product name
            if(m/\s*.*PRODUCT\s*=.*' (.*)'/x){ $product = $1; } 
            if(m/\s*.*\*.* package\s* (.*)/x){ $product = $1; } 

          # Release number
            if(m/\s*.*RELEASE\s*=.* (\d+.\d+)/x){ $ver = $1; }
            if(m/\s*.*_RELEASE\s*.* (\d+.\d+)/x){ $ver = $1; }

          # Development status
            if(m/\s*.*DEV_STATUS\s*=.*' (.*)'/x){ $devstat = $1; } 
            if(m/\s*.*_DEV_STATUS\s*.*'(.*)'/x){ $devstat = $1; } 

          # Development level
            if(m/\s*.*DEV_LEVEL\s*=.*' (.*)'/x){ $devlev = $1; } 
            if(m/\s*.*_DEV_LEVEL\s*.*' (.*)'/x){ $devlev = $1; } 

          # Build release
            if(m/\s*.*BUILD\s*=.*'\$Revision: \s*(.*)\s\s* \$'/x){ $build = $1; } 

          # Product codename
            if(m/\s*.*CODENAME\s*=.*' (.*)'/x){ $codename = $1; } 
            if(m/\s*.*_CODENAME\s*.*' (.*)'/x){ $codename = $1; } 

          # Version release date
            if(m/\s*.*RELDATE\s*=.*' (.*)'/x){ $reldate = $1; last; } 
            if(m/\s*.*_RELDATE\s*.*' (.*)'/x){ $reldate = $1; last; } 

          } # Close while
        close(FM);

  if($product) {
                 push @BADVERSIONS, "$file:$product:$ver:$devstat:$devlev:$build:$codename:$reldate";
                 } else { print "Nothing To See Here, Move Along The Bus Please!"; }

} # Close for

# Show what we have found
  if($emailresults == 1){ sendmail(); }
  if($outputresults == 1){ displayresults(); }

#-- SUBS --#
sub sendmail {
  $hostname = `hostname`;
  chomp($hostname);

  if ($product) {

    foreach (@BADVERSIONS){
      ($file,$cmsproduct,$cmsversion,$cmsdevstat,$cmsdevlev,$cmsbuild,$cmscodename,$cmsreldate) = split(":", $_, 8);
        $instances .= "    -----------------------------------------------------------------------------------\n";
        $instances .= "    $cmsproduct Version: $cmsversion.$cmsdevlev $cmsdevstat\n";
        $instances .= "    Build: $cmsbuild  Released: $cmsreldate  Codename: $cmscodename\n";
        ($newFile = $file) =~ s/includes\/version.php//ig;
        $instances .= "    $newFile\n";
        $instances .= "    ===================================================================================\n\n";
    } # Close for

  } else { $instances .= "    Nothing To See Here, Move Along Now Please!\n\n    >> No Instances Found! <<\n"; }

  $msg = "\n\nThe following installations were found...\n\n\n";

    open(SENDMAIL,"|/usr/sbin/sendmail -t");
      print SENDMAIL "To: <$admin_email>\n";
      print SENDMAIL "From: root\@$hostname\n";
      print SENDMAIL "Subject: $subject ($hostname)\n\n";
      print SENDMAIL "$msg $instances";
    close(SENDMAIL);
} # end sendmailsub

sub displayresults {

  foreach (@BADVERSIONS){
    ($file,$cmsproduct,$cmsversion,$cmsdevstat,$cmsdevlev,$cmsbuild,$cmscodename,$cmsreldate) = split(":", $_, 8);
      if($disablevulnscripts  == 1){ next; }
        print "$cmsproduct - $cmsversion - $cmsdevstat - $cmsdevlev - $cmsbuild - $cmscodename - $cmsreldate\n";
        ($newFile = $file) =~ s/includes\/version.php//ig;
        print "$newFile\n";
        print "--------------------------------------------------------------------------------------------------\n";
      }
  } # Close for

